﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class FlowStepModify : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserName"] == null || Session["roleID"] == null)
        {
            Response.Redirect("login.aspx");
            Response.End();
        }
        if (!Page.IsPostBack)
        {
            if (Request.QueryString["flowstepid"] != null)
            {
                int flowStepId = Convert.ToInt32(Request.QueryString["flowstepid"].ToString().Trim());
                BindToDdlDep();
                BindToDdlName();
                ddlToDep.Attributes.Add("onchange", "send_request()");                
                BindFlowStepMessage(flowStepId);
                
            }
            else
            {
                Response.Redirect("MessageInfo.aspx");
            }
        }

    }
    //绑定数据到DdlDep控件
    protected void BindToDdlDep()
    {
        DBOperation DBO = new DBOperation();
        string sqlString = "select DEPID,DEPNAME from DEPARTMENT where STATUS=1 order by DEPID ASC";
        SqlDataReader sdr = DBO.GetDataReader(sqlString);
        ddlToDep.DataSource = sdr;
        ddlToDep.DataTextField = "DEPNAME";
        ddlToDep.DataValueField = "DEPID";
        ddlToDep.DataBind();
        sdr.Close();

    }
    //绑定数据到DdlName控件
    protected void BindToDdlName()
    {
        DBOperation DBO = new DBOperation();
        string sqlString = "select TRUENAME,USERNAME from USERS where STATUS=1 and USERNAME<>'admin' and DEPID=" + ddlToDep.SelectedValue + " order by DEPID ASC";
        SqlDataReader sdr = DBO.GetDataReader(sqlString);
        ddlToUserName.DataSource = sdr;
        ddlToUserName.DataTextField = "TRUENAME";
        ddlToUserName.DataValueField = "USERNAME";
        ddlToUserName.DataBind();
        sdr.Close();

    }
    //实现部门与人员的二级联动
    protected void ddlDep_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindToDdlName();
    }
    protected void BindFlowStepMessage(int flowStepId)
    {
        FlowStep FS = new FlowStep(flowStepId);
        txtStepName.Text = FS.StepName;
        txtSEQ.Text = FS.SEQ.ToString();
        txtStepBrief.Text = FS.Content;
        lblFlowMID.Text = FS.FlowMID.ToString();
        lblFlowStepID.Text = FS.LCBZMID.ToString();
        ddlToDep.SelectedValue = FS.DepID.ToString();        
        BindToDdlName();
        ddlToUserName.SelectedValue = FS.UserIDList.ToString().Trim();
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //判断该步骤顺序号是否已被占用
        int flowStepId=Convert.ToInt32(lblFlowStepID.Text.Trim());
        string stepSEQ = txtSEQ.Text.ToString().Trim();
        string FlowMID = lblFlowMID.Text.ToString().Trim();
        string CheckSQL = "select count(*) FROM LCBZMINFO where LCBZMID!="+flowStepId.ToString()+" and SEQ=" + stepSEQ + " and FLOWMID='" + FlowMID + "'";
        BasicClass BC = new BasicClass();
        //步骤顺序号若已被占用，提示用户
        if (BC.CheckRecordExist(CheckSQL))
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('该流程步骤序号已被占用，请更改步骤序号!');", true);
        }
        //若未被占用，添加数据库记录，提示添加成功，重新绑定已添加步骤。
        else
        {
            FlowStep UpdateFStep = new FlowStep(flowStepId);
            UpdateFStep.FlowMID = Convert.ToInt32(FlowMID);
            UpdateFStep.StepName = txtStepName.Text;
            UpdateFStep.UserIDList = Request.Form["ddlToUserName"].ToString();
            UpdateFStep.DepID = Convert.ToInt32(ddlToDep.SelectedValue.ToString());
            UpdateFStep.SEQ = Convert.ToInt32(stepSEQ);
            UpdateFStep.Content = txtStepBrief.Text;
            if (UpdateFStep.UpdateFlowStep())
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('步骤修改成功!');location.href='FlowStepSet.aspx?flowid="+UpdateFStep.FlowMID.ToString()+"';", true);
            }
        }

    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        string FlowMID = lblFlowMID.Text.Trim();
        Response.Redirect("FlowStepSet.aspx?flowid=" + FlowMID);
    }
}
